package org.javaforever.cookieshop.dao;

import org.javaforever.cookieshop.domain.RecommendBean;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import jakarta.transaction.Transactional;
public interface RecommendDao extends JpaRepository<RecommendBean, Long>{
	@Query("from RecommendBean r")
	public List<RecommendBean> listAllRecommends() throws Exception;

	@Modifying
	@Query("delete from RecommendBean r where r.id = :id")
	@Transactional
	public void deleteRecommend(Long id) throws Exception;

	@Modifying
	@Query("update RecommendBean r set r.active = false where r.id = :id")
	@Transactional
	public void softDeleteRecommend(Long id) throws Exception;

	@Query("from RecommendBean r where r.id = :id")
	public RecommendBean findRecommendById(Long id) throws Exception;

	@Query("from RecommendBean r where r.name = :name")
	public RecommendBean findRecommendByName(String name) throws Exception;

	@Query("from RecommendBean r where r.active = true")
	public List<RecommendBean> listActiveRecommends() throws Exception;

	@Modifying
	@Query("update RecommendBean r set r.active = not r.active where r.id = :id")
	@Transactional
	public void toggleRecommend(Long id) throws Exception;

	@Modifying
	@Query("update RecommendBean r set r.active = true where r.id = :id")
	@Transactional
	public void activateRecommend(Long id) throws Exception;

	@Query("from RecommendBean r where r.id = :id")
	public RecommendBean findRecommendFullInfoById(Long id) throws Exception;

	@Query("from RecommendBean r where r.name like CONCAT(:shortName, '%')")
	public List<RecommendBean> searchRecommendIndexedNamesByShortName(String shortName) throws Exception;

	@Query("select count(*) as countNum from RecommendBean r where r.active = true")
	public Integer countActiveRecommendRecords() throws Exception;

}
